package com.david.func_test.view

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import androidx.viewpager2.widget.ViewPager2

class BottomLineTabLayout(context: Context, attributeSet: AttributeSet):FrameLayout(context, attributeSet) {
    val titles = ArrayList<String>()

    var lineHeight = 3
    var lineWidth = 10
    var lineColor = Color.RED
    var tabSpace = 20
    private var viewPager2: ViewPager2? = null
    private var lineView: View = View(context)

    init {
        lineView.setBackgroundColor(lineColor)
        lineView.layoutParams = LayoutParams(lineWidth, lineHeight)
    }

    private val onPageChangeCallback: ViewPager2.OnPageChangeCallback = object :
        ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
        }

        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int
        ) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels)
//            bgView.updateLayoutParams<MarginLayoutParams> { leftMargin =
//                (tabWidth* position + tabWidth * positionOffset).toInt()
//            }
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)

    }

    fun setupWithViewPager2(viewPager2: ViewPager2){
        viewPager2.registerOnPageChangeCallback(onPageChangeCallback)
        this.viewPager2 = viewPager2
    }
}